package com.dudu.controller;

import java.util.Date;
import java.util.List;

import net.sf.json.JSONObject;

import com.dudu.access.ChengQiUtil;
import com.dudu.access.RongLianUtils;
import com.dudu.access.TianRunUtil;
import com.dudu.access.YonYouUtil;
import com.dudu.bean.AppExtendBean;
import com.dudu.bean.DialResultBean;
import com.dudu.bean.PoolAbilityInfoBean;
import com.dudu.bean.ResponseSendVoiceBean;
import com.dudu.service.IAppExtendService;
import com.dudu.service.IDataCompleteService;
import com.dudu.service.IPoolNumberService;
import com.dudu.service.ISmsService;
import com.dudu.service.impl.AppExtendServiceImpl;
import com.dudu.service.impl.DataCompleteServiceImpl;
import com.dudu.service.impl.PoolNumberServiceImpl;
import com.dudu.service.impl.SmsServiceImpl;
import com.dudu.utils.DateUtils;
import com.dudu.utils.DictionaryUtils;

import core.CoreService;

/**
 * 监控通道
 * 
 * @author gaoxs
 * 
 */
public class JianKongTicketController extends CoreService implements IJianKongTicketController {
	private String called = DictionaryUtils.getValueByTypeAndKey("jiankong", "called");
	private String caller = DictionaryUtils.getValueByTypeAndKey("jiankong", "caller");
	private IPoolNumberService poolNumberService = new PoolNumberServiceImpl();
	private ISmsService smsService = new SmsServiceImpl();
	private IDataCompleteService dataCompleteService = new DataCompleteServiceImpl();
	private IAppExtendService appExtendService = new AppExtendServiceImpl();

	public String getDisplayNum(String ability_Type) {
		PoolAbilityInfoBean poolAbilityInfo = poolNumberService.queryDefaultPoolAbilityNumber(ability_Type, "general");// 公共号码池
		if (poolAbilityInfo != null) {
			return poolAbilityInfo.getDisplay_number();
		}
		return "";
	}

	public String openApi() {
		JSONObject object = new JSONObject();
		String app_key = this.getRequest().getParameter("app_key");
		AppExtendBean appExtendBean = appExtendService.queryAppExtendByIdentify(app_key);
		if (appExtendBean != null) {
			object.put("result", 0);
			object.put("Key_private", appExtendBean.getKey_private());
			object.put("Key_public", appExtendBean.getKey_public());
		} else {
			object.put("result", -1);
		}
		return returnData(object.toString());
	}

	/**
	 * 荣联语音通知 result=0 成功 result=-1 失败
	 */
	public String ronglianVoiceNotice() {
		JSONObject object = new JSONObject();
		ResponseSendVoiceBean result = RongLianUtils.VoiceNotice(called, "", "监控测试", "0", "01062434320", "", "");
		if ("0".equals(result.getResult())) {
			object.put("result", 0);
		} else {
			object.put("result", -1);
		}
		object.put("sessionid", result.getSessionid());
		// object.put("desc", result.getDesc()); zabbix不支持中文
		return returnData(object.toString());
	}

	/**
	 * 荣联语音调研 result=0 成功 result=-1 失败
	 */
	public String ronglianIvrDial() {
		JSONObject object = new JSONObject();
		ResponseSendVoiceBean result = RongLianUtils.IvrDial(called, "01062434320", "", "", "", "监控测试,满意请按1;不满意请按2",
				"", "1", "", "1");
		if ("0".equals(result.getResult())) {
			object.put("result", 0);
		} else {
			object.put("result", -1);
		}
		object.put("sessionid", result.getSessionid());
		object.put("desc", result.getDesc());
		return returnData(object.toString());
	}

	/**
	 * 荣联两方通道 result=0 成功 result=-1 失败
	 */
	public String ronglianDial() {
		JSONObject object = new JSONObject();
		DialResultBean result = RongLianUtils.dial(caller, called, "01062434320", "", "0", "0");
		if ("0".equals(result.getResult())) {
			object.put("result", 0);
		} else {
			object.put("result", -1);
		}
		object.put("sessionid", result.getSessionId());
		object.put("desc", result.getDesc());
		return returnData(object.toString());
	}

	/**
	 * 天润融通语音通知 result=0 成功 result=-1 失败
	 */
	public String tianRunVoiceNotice() {
		JSONObject object = new JSONObject();
		String tianRunDisplayNum = this.getDisplayNum("TianRunVoice");
		ResponseSendVoiceBean result = TianRunUtil.voiceNotice(called, "监控测试", tianRunDisplayNum, "", "");
		if ("0".equals(result.getResult())) {
			object.put("result", 0);
		} else {
			object.put("result", -1);
		}
		object.put("sessionid", result.getSessionid());
		object.put("desc", result.getDesc());
		return returnData(object.toString());
	}

	/**
	 * 自建通道语音通知 result=0 成功 result=-1 失败
	 */
	public String yonYouVoiceNotice() {
		JSONObject object = new JSONObject();
		String yonyouDisplayNum = this.getDisplayNum("YonYouVoice");
		ResponseSendVoiceBean result = YonYouUtil.VoiceNotice(caller, called, "", "监控测试", yonyouDisplayNum, "", "", "",
				"1", "1");
		if ("0".equals(result.getResult())) {
			object.put("result", 0);
		} else {
			object.put("result", -1);
		}
		object.put("sessionid", result.getSessionid());
		object.put("desc", result.getDesc());
		return returnData(object.toString());
	}

	/**
	 * 自建通道语音调研 result=0 成功 result=-1 失败
	 */
	public String yonYouIvrDail() {
		JSONObject object = new JSONObject();
		String yonyouDisplayNum = this.getDisplayNum("Ivr_YonYou");
		ResponseSendVoiceBean result = YonYouUtil
				.IvrDial(caller, called, yonyouDisplayNum, "", "监控测试,满意请按1;不满意请按2", "");
		if ("0".equals(result.getResult())) {
			object.put("result", 0);
		} else {
			object.put("result", -1);
		}
		object.put("sessionid", result.getSessionid());
		object.put("desc", result.getDesc());
		return returnData(object.toString());
	}

	/**
	 * 自建通道两方通话 result=0 成功 result=-1 失败
	 */
	public String yonYouDail() {
		JSONObject object = new JSONObject();
		String yonyouDisplayNum = this.getDisplayNum("Dial_YonYou");
		DialResultBean result = YonYouUtil.dial(caller, called, yonyouDisplayNum, "0");
		if ("0".equals(result.getResult())) {
			object.put("result", 0);
		} else {
			object.put("result", -1);
		}
		object.put("sessionid", result.getSessionId());
		object.put("desc", result.getDesc());
		return returnData(object.toString());
	}

	/**
	 * 承启通两方通道 result=0 成功 result=-1 失败
	 */
	public String chengQiDail() {
		JSONObject object = new JSONObject();
		String chengQiDisplayNum = this.getDisplayNum("Dial_chengqi");
		DialResultBean result = ChengQiUtil.dial(caller, called, chengQiDisplayNum, "0");
		if ("0".equals(result.getResult())) {
			object.put("result", 0);
		} else {
			object.put("result", -1);
		}
		object.put("sessionid", result.getSessionId());
		object.put("desc", result.getDesc());
		return returnData(object.toString());
	}

	/**
	 * 短信验证码延时数量
	 * 
	 * @return
	 */
	public String smsCodeDelay() {
		JSONObject object = new JSONObject();
		int delayAmount = smsService.queryDelayAmounts();
		object.put("result", delayAmount);
		return returnData(object.toString());
	}

	/**
	 * 短信验证码发送失败数量
	 * 
	 * @return
	 */
	public String smsCodeError() {
		JSONObject object = new JSONObject();
		int errorAmount = smsService.queryErrorAmounts();
		object.put("result", errorAmount);
		return returnData(object.toString());
	}
	
	/**
	 * 短信验证码下发失败率
	 * 一小时失败率超过20%时报警
	 * 20%可在后台配置
	 */
	public String smsCodeErrorPercent() {
		JSONObject object = new JSONObject();
		double errorAmount = smsService.queryErrorPercent();
		object.put("result", errorAmount);
		return returnData(object.toString());
	}
	

	/**
	 * 校验状态数据是否完整，调用通道接口成功30分钟后应该有状态数据 IMS多方通话、自建通道
	 */
	@Override
	public String statusCompleteCheck() {
		JSONObject object = new JSONObject();
		Date now = new Date();
		Date now_30 = new Date(now.getTime() - 30 * 60 * 1000); // 30分钟前的时间
		Date now_35 = new Date(now.getTime() - 35 * 60 * 1000); // 35分钟前的时间
		String beginTime = DateUtils.getFormatTime(now_35, "yyyy-MM-dd HH:mm:ss");
		String endTime = DateUtils.getFormatTime(now_30, "yyyy-MM-dd HH:mm:ss");
		List<JSONObject> list = dataCompleteService.queryStatusAmounts(beginTime, endTime);
		if (list != null && list.size() > 0) {
			object.put("result", list.size());
		} else {
			object.put("result", 0);
		}
		return returnData(object.toString());
	}

	/**
	 * 校验状态数据是否完整，调用通道接口成功30分钟后应该有状态数据 IMS多方通话、自建通道
	 */
	@Override
	public String billCompleteCheck() {
		JSONObject object = new JSONObject();
		Date now = new Date();
		Date now_30 = new Date(now.getTime() - 30 * 60 * 1000); // 30分钟前的时间
		Date now_35 = new Date(now.getTime() - 35 * 60 * 1000); // 35分钟前的时间
		String beginTime = DateUtils.getFormatTime(now_35, "yyyy-MM-dd HH:mm:ss");
		String endTime = DateUtils.getFormatTime(now_30, "yyyy-MM-dd HH:mm:ss");
		List<JSONObject> list = dataCompleteService.queryBillAmounts(beginTime, endTime);
		if (list != null && list.size() > 0) {
			object.put("result", list.size());
		} else {
			object.put("result", 0);
		}
		return returnData(object.toString());
	}

	/**
	 * 查询语音通知、语音调研积压发送数量
	 */
	@Override
	public String queryVoiceDelayAmounts() {
		JSONObject object = new JSONObject();
		Date now = new Date();
		Date now_30 = new Date(now.getTime() - 30 * 60 * 1000); // 30分钟前的时间
		Date now_35 = new Date(now.getTime() - 35 * 60 * 1000); // 35分钟前的时间
		String beginTime = DateUtils.getFormatTime(now_35, "yyyy-MM-dd HH:mm:ss");
		String endTime = DateUtils.getFormatTime(now_30, "yyyy-MM-dd HH:mm:ss");
		List<JSONObject> list = dataCompleteService.queryVoiceDelayAmounts(beginTime, endTime);
		if (list != null && list.size() > 0) {
			object.put("result", list.size());
		} else {
			object.put("result", 0);
		}
		return returnData(object.toString());
	}
}
